/*$Id: DecimalRange.java 66 2008-03-04 21:06:48Z lrsmith $*/
package com.leetech.gwt.client;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Composite;



public class DecimalRange extends Composite {

    // Static initialization-------------------------------------------
    public static String CSS_STATE_INVALID = "state-invalid";
    // Instance variables----------------------------------------------
    ValidatedTextBox lowerBoundBox;
    ValidatedTextBox upperBoundBox;
    int state;
    String name;


    // Constructors----------------------------------------------------
	/**
	 * Construct a DecimalRange widget.
	 */
    public DecimalRange() {
        VerticalPanel outer= new VerticalPanel();
        Label lowerLabel = new Label("greater than");
        Label upperLabel = new Label("and less than");
        lowerBoundBox = new ValidatedTextBox("\\s*[-+]?\\d+(\\.\\d*)?\\s*");
        upperBoundBox = new ValidatedTextBox("\\s*[-+]?\\d+(\\.\\d*)?\\s*");
        outer.add(lowerLabel);
        outer.add(lowerBoundBox);
        outer.add(upperLabel);
        outer.add(upperBoundBox);

        initWidget(outer);
    }

    // Methods---------------------------------------------------------
    public int getValidationState(){
        if(lowerBoundBox.getValidationState() == ValidatedTextBox.STATE_INVALID ||
                upperBoundBox.getValidationState() == ValidatedTextBox.STATE_INVALID ){
            setStateInvalid();
            return ValidatedTextBox.STATE_INVALID;
        }else if(lowerBoundBox.getValidationState() == ValidatedTextBox.STATE_EMPTY &&
                upperBoundBox.getValidationState() == ValidatedTextBox.STATE_EMPTY){
            setStateEmpty();
            return ValidatedTextBox.STATE_EMPTY;
        }else if(lowerBoundBox.getValidationState() == ValidatedTextBox.STATE_EMPTY ||
                upperBoundBox.getValidationState() == ValidatedTextBox.STATE_EMPTY){
            setStateValid();
            return ValidatedTextBox.STATE_VALID;
        }else if(getMinValue() > getMaxValue()){
            setStateInvalid();
            return ValidatedTextBox.STATE_INVALID;
        }else {
            setStateValid();
            return ValidatedTextBox.STATE_VALID;
        }
    }
    /**
     * Returns false if the state is invalid, otherwise returns true for empty
     * or valid state.  
     */
    public boolean isValidState(){
        if(getValidationState() == ValidatedTextBox.STATE_INVALID){
            return false;
        }else{
            return true;
        }
    }
       
    private void setStateInvalid() {
        state = ValidatedTextBox.STATE_INVALID;
        addStyleDependentName(CSS_STATE_INVALID);
    }

    private void setStateValid() {
        removeStyleDependentName(CSS_STATE_INVALID);
        state = ValidatedTextBox.STATE_VALID;
    }

    private void setStateEmpty() {
        removeStyleDependentName(CSS_STATE_INVALID);
        state = ValidatedTextBox.STATE_EMPTY;
    }

    public double getMinValue() {
            if(lowerBoundBox.getValidationState() == ValidatedTextBox.STATE_VALID){
                return Double.parseDouble(lowerBoundBox.getText());
            }
            if(lowerBoundBox.getValidationState() == ValidatedTextBox.STATE_EMPTY){
                return Double.MIN_VALUE;
            }else{
                return Double.NaN;
            }
    }

    public double getMaxValue() {
            if(upperBoundBox.getValidationState() == ValidatedTextBox.STATE_VALID){
                return Double.parseDouble(lowerBoundBox.getText());
            }
            if(lowerBoundBox.getValidationState() == ValidatedTextBox.STATE_EMPTY){
                return Double.MAX_VALUE;
            }else{
                return Double.NaN;
            }
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    // Inner Classes --------------------------------------------------

}
